package cn.e4j.modular.system.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.cache.CacheKey;
import cn.e4j.core.cache.CacheUtil;
import cn.e4j.core.common.annotion.BussinessLog;
import cn.e4j.core.common.annotion.Permission;
import cn.e4j.core.common.constant.Const;
import cn.e4j.core.common.constant.dictmap.UserDict;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.core.util.TreeUtil;
import cn.e4j.model.exception.ServiceException;
import cn.e4j.modular.system.entity.SysCmp;
import cn.e4j.modular.system.entity.SysModule;
import cn.e4j.modular.system.mapper.SysCmpMapper;
import cn.e4j.modular.system.service.SysCmpService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 界面组件管理
 *
 * @author wenchuan.li
 * @Date 2018/12/08 16:03
 */
@Controller
@RequestMapping("/sys/cmp")
public class SysCmpController extends BaseController {

    @Autowired
    private SysCmpMapper sysCmpMapper;

    @Autowired
    private SysCmpService sysCmpService;

    private final static String[][] strings = new String[][]{
            {"新增","btn_add","add"},
            {"删除","btn_del","del"},
            {"修改","btn_edit","edit"},
            {"作废","btn_trash","trash"},
            {"导出Excel","btn_export","export"}
    };

    /**
     * 跳转界面组件管理页面
     */
    @RequiresPermissions("sys:cmp:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/system/cmp.html";
    }

    /**
     * 跳转到查看新增页面
     */
    @RequestMapping("/cmp_add")
    public String addView() {
        return "/pc/system/cmp_edit.html";
    }

    /**
     * 查询界面组件信息列表
     */
    @RequestMapping("/getCmplist")
    @RequiresPermissions("sys:cmp:init")
    @ResponseBody
    public ResponseData getCmplist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = sysCmpService.getCmplist(page, inDto);
        return new PageReData(page, result);
    }


    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("sys:cmp:init")
    @ResponseBody
    public ResponseData edit(SysCmp cmp, BindingResult result) {
        cmp.updateById();

        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("sys:cmp:init")
    @ResponseBody
    public ResponseData del(SysCmp cmp, BindingResult result) {
        cmp.deleteById();

        return SUCCESS_TIP;
    }


    /**
     * 添加
     */
    @RequestMapping("/add")
    @RequiresPermissions("sys:cmp:init")
    @ResponseBody
    public ResponseData add(SysCmp cmp, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        cmp.insert();
        return SUCCESS_TIP;
    }

    /**
     * 初始化界面组件
     */
    @RequestMapping("/initUI")
    @ResponseBody
    public ResponseData initUI(@RequestParam String pageId) {
        if (ToolUtil.isEmpty(pageId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        SysModule sysModule = this.sysCmpService.selectModuleByKey(pageId);
        StringBuffer prix=new StringBuffer("");
        if(StringUtils.isNotEmpty(sysModule.getURL())){
            for(String s:sysModule.getURL().trim().split("/")){
                if(StringUtils.isNotEmpty(s))prix.append(s).append(":");
            }
        }
        SysCmp cmp = new SysCmp();
        this.sysCmpMapper.delByPageId(pageId);
        for (String[] s:strings){
            cmp.setDOM_TYPE("1");
            cmp.setNAME(s[0]);
            cmp.setDOM_ID(s[1]);
            cmp.setDOM_PERSSION(prix+s[2]);
            cmp.setPAGE_ID(pageId);
            cmp.insert();
        }
        return SUCCESS_TIP;
    }

    @RequestMapping(value="/exportsCmp",method= RequestMethod.GET)
    @RequiresPermissions("sys:cmp:init")
    public void exportsUser(ModelMap map, HttpServletRequest request, HttpServletResponse response){
        Dto inDto = Dtos.newDto();
        List<SysCmp> list = sysCmpService.getCmplist(inDto);
        ExportParams params = new ExportParams(null, "界面组件", ExcelType.XSSF);
        //params.setFreezeCol(2);
        map.put(NormalExcelConstants.FILE_NAME, "界面组件");
        map.put(NormalExcelConstants.DATA_LIST, list);
        map.put(NormalExcelConstants.CLASS, SysCmp.class);
        map.put(NormalExcelConstants.PARAMS, params);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

}
